﻿#region << 文件说明 >>
/*----------------------------------------------------------------
 * 命名空间：Vampirewal.BlogCore.Controls.Container
 * 唯一标识：e44e17f3-7b2e-45ea-bd6b-c7f3e11f99f7
 * 文件名：Carousel
 * 
 * 创建者：杨程
 * 电子邮箱：235160615@qq.com
 * 创建时间：2024/7/25 14:54:54
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/
#endregion << 文件说明 >>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Vampirewal.BlogCore;

/// <summary>
/// 走马灯控件
/// </summary>
public sealed class Carousel:ContainerControlBase<CarouselItem>
{
    /// <summary>   
    /// 构造函数
    /// </summary>
    internal Carousel()
    {
        Children = new List<CarouselItem>();
    }

    public override List<CarouselItem> Children { get; internal set; }
    public override string Id { get; set; }
    public override string Title { get; set; }

    public override string ConverToHtml()
    {
        StringBuilder sb = new StringBuilder();

        var heightValue = string.Empty;
        if(Height is int heightInt)
        {
            heightValue = $"height=\"{heightInt}px\"";
        }
        else if (Height is string heightStr)
        {
            heightValue = $"height=\"{heightStr}\"";
        }

        sb.AppendLine($"<el-carousel {heightValue} type=\"card\">");
        foreach (var item in Children)
        {
            //sb.AppendLine($"<el-carousel-item style=\"height: 100px\">");

            sb.AppendLine(item.ConverToHtml());

            //sb.AppendLine($"<el-carousel-item/>");
        }
        sb.AppendLine($"</el-carousel>");

        return sb.ToString();
    }

    /// <summary>
    /// 高度
    /// </summary>
    internal object Height { get; set; }
}

/// <summary>
/// 走马灯项
/// </summary>
public sealed class CarouselItem : ControlBase
{
    internal CarouselItem()
    {
        
    }

    public override string Id { get; set; }
    public override string Title { get; set; }

    public override string ConverToHtml()
    {
        StringBuilder sb = new StringBuilder();

        sb.AppendLine($"<el-carousel-item style=\"height: {Height}px\">");

        sb.AppendLine(Content.ConverToHtml());

        sb.AppendLine($"</el-carousel-item>");

        return sb.ToString();
    }
    /// <summary>
    /// 内容
    /// </summary>
    internal ControlBase Content { get; set; }

    /// <summary>
    /// 高度
    /// </summary>
    internal int Height { get; set; }
}


public static partial class App
{
    /// <summary>
    /// 创建走马灯容器
    /// </summary>
    /// <param name="height"></param>
    /// <param name="action"></param>
    /// <returns></returns>
    /// <exception cref="Exception"></exception>
    public static Carousel CreateCarousel(int height,Action<List<CarouselItem>> action)
    {
        if (action == null) throw new Exception("走马灯内容项不能为空");

        var carousel = new Carousel()
        {
            Id=GenerateId(),
            Title="走马灯",
            Height= height
        };

        action?.Invoke(carousel.Children);

        return carousel;
    }

    /// <summary>
    /// 创建走马灯
    /// </summary>
    /// <param name="height"></param>
    /// <param name="action"></param>
    /// <returns></returns>
    /// <exception cref="Exception"></exception>
    public static Carousel CreateCarousel(string height, Action<List<CarouselItem>> action)
    {
        if (action == null) throw new Exception("走马灯内容项不能为空");

        var carousel = new Carousel()
        {
            Id = GenerateId(),
            Title = "走马灯",
            Height = height
        };

        action?.Invoke(carousel.Children);

        return carousel;
    }

    /// <summary>
    /// 创建走马灯内容
    /// </summary>
    /// <param name="height"></param>
    /// <param name="func"></param>
    /// <returns></returns>
    /// <exception cref="Exception"></exception>
    public static CarouselItem CreateCarouselItem(int height,Func<ControlBase> func)
    {
        if (height <= 0) height = 100;

        var item = new CarouselItem()
        {
            Height= height
        };

        var result = func?.Invoke();
        if(result == null)
            throw new Exception("创建内容不能为空！");

        item.Content = result;

        return item;
    }
}